//
//  MassTextingVC.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/1.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MassTextingVC.h"
#import "UIView+Layer.h"
#import "CameraActionSheet.h"
#import "VPImageCropperViewController.h"
#import "HTTPGlobals.h"
#import "AFHTTPRequestOperation.h"

#define CGRectHeadViewHeight  44

@interface MassTextingVC ()<UIScrollViewDelegate,UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,CameraActionSheetDelegate,UITextFieldDelegate>
{
    NSMutableArray *BtnArr;            /**< 存放menu按钮的数组*/
    UIView *lineView;                  /**< 随菜单滚动的下划线*/
    UIScrollView *_ScrollView;         /**<                */
    int offset;                     /**< 当前的偏移量     */
    UITableView *_tableView;
    UIView *_headView;
    CGFloat _oldOffsetY;             /**< scrollView的偏移量 */
    CGFloat _oldOffsetX;
    UIButton *SelectBtn;             /**< 选中按钮     */
}
@end

@implementation MassTextingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"群发";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];

    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame) - 20)];
    [self AllocMenu];
    [self AllocScrollView];
    [_tableView setTableHeaderView:_headView];
    
}

#pragma mark --初始化scrollView--
-(void)AllocScrollView {
    
    _ScrollView = [[UIScrollView alloc] init];
    _ScrollView.frame = CGRectMake(0, CGRectHeadViewHeight + 2, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) + 60 );
    _ScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*4, 0);
    _ScrollView.showsHorizontalScrollIndicator = NO;
    _ScrollView.showsVerticalScrollIndicator = NO;
    _ScrollView.pagingEnabled=YES;
    _ScrollView.delegate = self;
//    _ScrollView.contentInset = UIEdgeInsetsMake(20, 0, 20, 0);
    
    [_headView addSubview:_ScrollView];
    [self SetTextView];
    [self SetImgView];
    [self SetNewsView];
    [self SetMoreNewsView];
}
#pragma mark --加载文字页面--
-(void)SetTextView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(_ScrollView.frame), 100)];
    view.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 20)];
    label.text = @"文字内容:";
    UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label.frame)*2, 30)];
    textfield.placeholder = @"文字输入";
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    textfield.tag = 1000;
    textfield.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(CGRectGetMinX(textfield.frame), CGRectGetMaxY(textfield.frame) + 10, 50, 30);
    [btn setTitle:@"群发" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [UIView SetRoundedCorners:btn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(TextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [view addSubview:label];
    [view addSubview:textfield];
    [view addSubview:btn];
    [_ScrollView addSubview:view];
    
}
#pragma mark --加载图片页面--
-(void)SetImgView {
    UIView *BGview = [[UIView alloc] initWithFrame:CGRectMake(_ScrollView.frame.size.width, 10, CGRectGetWidth(_ScrollView.frame), 100)];
    BGview.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 20)];
    label.text = @"选择一张图片:";
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), CGRectGetWidth(BGview.frame) - CGRectGetMinX(label.frame)*2, 30);
    [imageBtn setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:104/255.0 blue:107/255.0 alpha:0.9];
    [imageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [UIView SetRoundedCorners:imageBtn andcornerRadius:5.0 andborderWidth:1 andborderColor:[UIColor clearColor]];
    imageBtn.tag = 1001;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(CGRectGetMinX(imageBtn.frame), CGRectGetMaxY(imageBtn.frame) + 10, 50, 30);
    [btn setTitle:@"群发" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [UIView SetRoundedCorners:btn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(ImgaeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [BGview addSubview:label];
    [BGview addSubview:imageBtn];
    [BGview addSubview:btn];
    [_ScrollView addSubview:BGview];
}
#pragma mark --加载单条新闻页面--
-(void)SetNewsView {
    UIView *BGview = [[UIView alloc] initWithFrame:CGRectMake(_ScrollView.frame.size.width*2, 10, CGRectGetWidth(_ScrollView.frame), 100)];
    BGview.backgroundColor = [UIColor lightGrayColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 20)];
    label1.text = @"新闻标题:";
    UITextField *textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label1.frame), CGRectGetMaxY(label1.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label1.frame)*2, 30)];
    textfield1.placeholder = @"文字输入";
    textfield1.borderStyle = UITextBorderStyleRoundedRect;
    textfield1.tag = 2000;
    textfield1.delegate = self;
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield1.frame), CGRectGetMaxY(textfield1.frame) + 10, 200, 20)];
    label2.text = @"新闻缩略图";
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = CGRectMake(CGRectGetMinX(label2.frame), CGRectGetMaxY(label2.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label2.frame)*2, 30);
    [imageBtn setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:104/255.0 blue:107/255.0 alpha:0.9];
    [imageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn.tag = 2003;
    [UIView SetRoundedCorners:imageBtn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn.frame), CGRectGetMaxY(imageBtn.frame) + 10, 200, 20)];
    label3.text = @"新闻简介";
    
    UITextField *textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label3.frame), CGRectGetMaxY(label3.frame) + 5, CGRectGetWidth(_ScrollView.frame) -CGRectGetMinX(label3.frame)*2, 30)];
    textfield3.placeholder = @"文字输入";
    textfield3.borderStyle = UITextBorderStyleRoundedRect;
    textfield3.tag = 2001;
    textfield3.delegate = self;
    
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield3.frame), CGRectGetMaxY(textfield3.frame) + 10, 200, 20)];
    label4.text = @"新闻详情";
    
    UITextField *textfield4 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label4.frame), CGRectGetMaxY(label4.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label4.frame)*2, 30)];
    textfield4.placeholder = @"文字输入";
    textfield4.borderStyle = UITextBorderStyleRoundedRect;
    textfield4.tag = 2002;
    textfield4.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(CGRectGetMinX(textfield4.frame), CGRectGetMaxY(textfield4.frame) + 10, 50, 30);
    [btn setTitle:@"群发" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [UIView SetRoundedCorners:btn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(OneNewsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    BGview.frame = CGRectMake(_ScrollView.frame.size.width*2, 10, CGRectGetWidth(_ScrollView.frame), CGRectGetMaxY(btn.frame) + 10);
    
    [BGview addSubview:label1];
    [BGview addSubview:textfield1];
    [BGview addSubview:label2];
    [BGview addSubview:imageBtn];
    [BGview addSubview:label3];
    [BGview addSubview:textfield3];
    [BGview addSubview:label4];
    [BGview addSubview:textfield4];
    [BGview addSubview:btn];
    [_ScrollView addSubview:BGview];
}
#pragma mark --加载多条新闻页面--
-(void)SetMoreNewsView {
    UIView *BGview = [[UIView alloc] initWithFrame:CGRectMake(_ScrollView.frame.size.width*3, 10, CGRectGetWidth(_ScrollView.frame), 100)];
    BGview.backgroundColor = [UIColor lightGrayColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 20)];
    label1.text = @"新闻标题:";
    UITextField *textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label1.frame), CGRectGetMaxY(label1.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label1.frame)*2, 30)];
    textfield1.placeholder = @"文字输入";
    textfield1.borderStyle = UITextBorderStyleRoundedRect;
    textfield1.tag = 3000;
    textfield1.delegate = self;
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield1.frame), CGRectGetMaxY(textfield1.frame) + 10, 200, 20)];
    label2.text = @"新闻缩略图";
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = CGRectMake(CGRectGetMinX(label2.frame), CGRectGetMaxY(label2.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label2.frame)*2, 30);
    [imageBtn setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:104/255.0 blue:107/255.0 alpha:0.9];
    [imageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn.tag = 3006;
    [UIView SetRoundedCorners:imageBtn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn.frame), CGRectGetMaxY(imageBtn.frame) + 10, 200, 20)];
    label3.text = @"新闻简介";
    
    UITextField *textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label3.frame), CGRectGetMaxY(label3.frame) + 5, CGRectGetWidth(_ScrollView.frame) -CGRectGetMinX(label3.frame)*2, 30)];
    textfield3.placeholder = @"文字输入";
    textfield3.borderStyle = UITextBorderStyleRoundedRect;
    textfield3.tag = 3001;
    textfield3.delegate = self;
    
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield3.frame), CGRectGetMaxY(textfield3.frame) + 10, 200, 20)];
    label4.text = @"新闻详情";
    
    UITextField *textfield4 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label4.frame), CGRectGetMaxY(label4.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label4.frame)*2, 30)];
    textfield4.placeholder = @"文字输入";
    textfield4.borderStyle = UITextBorderStyleRoundedRect;
    textfield4.tag = 3002;
    textfield4.delegate = self;
    
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield4.frame), CGRectGetMaxY(textfield4.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label4.frame)*2, 20)];
    label5.text = @"新闻标题:";
    UITextField *textfield5 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label5.frame), CGRectGetMaxY(label5.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label5.frame)*2, 30)];
    textfield5.placeholder = @"文字输入";
    textfield5.borderStyle = UITextBorderStyleRoundedRect;
    textfield5.tag = 3003;
    textfield5.delegate = self;
    
    UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield5.frame), CGRectGetMaxY(textfield5.frame) + 10, 200, 20)];
    label6.text = @"新闻缩略图";
    UIButton *imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn1.frame = CGRectMake(CGRectGetMinX(label6.frame), CGRectGetMaxY(label6.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label6.frame)*2, 30);
    [imageBtn1 setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn1.backgroundColor = [UIColor colorWithRed:255/255.0 green:104/255.0 blue:107/255.0 alpha:0.9];
    [imageBtn1 addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn1.tag = 3007;
    [UIView SetRoundedCorners:imageBtn1 andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    
    UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn1.frame), CGRectGetMaxY(imageBtn1.frame) + 10, 200, 20)];
    label7.text = @"新闻简介";
    
    UITextField *textfield7 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label7.frame), CGRectGetMaxY(label7.frame) + 5, CGRectGetWidth(_ScrollView.frame) -CGRectGetMinX(label7.frame)*2, 30)];
    textfield7.placeholder = @"文字输入";
    textfield7.borderStyle = UITextBorderStyleRoundedRect;
    textfield7.tag = 3004;
    textfield7.delegate = self;
    
    UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield7.frame), CGRectGetMaxY(textfield7.frame) + 10, 200, 20)];
    label8.text = @"新闻详情";
    
    UITextField *textfield8 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label8.frame), CGRectGetMaxY(label8.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label8.frame)*2, 30)];
    textfield8.placeholder = @"文字输入";
    textfield8.borderStyle = UITextBorderStyleRoundedRect;
    textfield8.tag = 3005;
    textfield8.delegate  =self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(CGRectGetMinX(textfield8.frame), CGRectGetMaxY(textfield8.frame) + 10, 50, 30);
    [btn setTitle:@"群发" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [UIView SetRoundedCorners:btn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(MoreNewsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    BGview.frame = CGRectMake(_ScrollView.frame.size.width*3, 10, CGRectGetWidth(_ScrollView.frame), CGRectGetMaxY(btn.frame) + CGRectGetHeight(btn.frame) + 50);
    
    [BGview addSubview:label1];
    [BGview addSubview:textfield1];
    [BGview addSubview:label2];
    [BGview addSubview:imageBtn];
    [BGview addSubview:label3];
    [BGview addSubview:textfield3];
    [BGview addSubview:label4];
    [BGview addSubview:textfield4];
    [BGview addSubview:label5];
    [BGview addSubview:textfield5];
    [BGview addSubview:label6];
    [BGview addSubview:imageBtn1];
    [BGview addSubview:label7];
    [BGview addSubview:textfield7];
    [BGview addSubview:label8];
    [BGview addSubview:textfield8];
    [BGview addSubview:btn];
    [_ScrollView addSubview:BGview];
}
#pragma mark --调用相册，相机按钮--
-(void)ImageViewBtnAction:(UIButton *)sender {
    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
    [actionSheet show];
    SelectBtn = sender;
}
#pragma mark --文字发送按钮--
-(void)TextBtnAction:(UIButton *)sender {
    UITextField *TF = (UITextField *)[self.view viewWithTag:1000];
    if (TF.text.length <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"请输入发送内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    [self.HUD show:YES];
    NSDictionary *parame = [NSDictionary dictionaryWithObjectsAndKeys:TF.text,@"word_content", nil];
    NSString *path = @"MobilePort/Index/group_word.html";
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:path parameters:parame finish:^(BOOL seccess, NSString *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            [self showText:@"发送成功"];
            DLog(@"文字群发 obj = %@",obj);
            DLog(@"info = %@",obj[@"info"]);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
        }
    }];
}
#pragma mark --图片发送按钮--
-(void)ImgaeBtnAction:(UIButton *)sender {
    UIButton *btn = (UIButton *)[self.view viewWithTag:1001];
    UIImage *image = [btn backgroundImageForState:UIControlStateNormal];
    if (!image) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"请选择图片发送!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [self.HUD show:YES];
    NSString *path = @"MobilePort/Index/group_pic.html";
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path image:image imageName:@"pic_file" parameters:nil finish:^(BOOL seccess, NSError *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            [self showText:@"上传成功"];
            DLog(@"上传成功!");
            DLog(@"obj.info = %@",obj[@"info"]);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
        }
    }];
    
    
}
#pragma mark --单条新闻发送按钮--
-(void)OneNewsBtnAction:(UIButton *)sender {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:2000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:2001];
    UITextField *TF_3 = (UITextField *)[self.view viewWithTag:2002];
    UIButton *btn = (UIButton *)[self.view viewWithTag:2003];
    UIImage *image = [btn backgroundImageForState:UIControlStateNormal];
    if (TF_1.text.length <= 0 || TF_2.text.length <= 0 || TF_3.text.length <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"内容不能为空!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (!image) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"图片不能为空!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [self.HUD show: YES];
    NSDictionary *parame = [NSDictionary dictionaryWithObjectsAndKeys:TF_1.text,@"news_title",TF_2.text,@"news_desc",TF_3.text,@"news_content", nil];
    NSString *path = @"MobilePort/Index/group_news.html";
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path image:image imageName:@"news_img" parameters:parame finish:^(BOOL seccess, NSError *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            [self showText:@"新闻上传成功"];
            DLog(@"上传单条新闻成功!");
            DLog(@"obj = %@",obj);
            DLog(@"info = %@",obj[@"info"]);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
        }
    }];
    
}
#pragma mark --多条新闻发送按钮--
-(void)MoreNewsBtnAction:(UIButton *)sender {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:3000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:3001];
    UITextField *TF_3 = (UITextField *)[self.view viewWithTag:3002];
    UITextField *TF_4 = (UITextField *)[self.view viewWithTag:3003];
    UITextField *TF_5 = (UITextField *)[self.view viewWithTag:3004];
    UITextField *TF_6 = (UITextField *)[self.view viewWithTag:3005];
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:3006];
    UIButton *btn2 = (UIButton *)[self.view viewWithTag:3007];
    UIImage *image1 = [btn1 backgroundImageForState:UIControlStateNormal];
    UIImage *image2 = [btn2 backgroundImageForState:UIControlStateNormal];
    if (TF_1.text.length <= 0 || TF_2.text.length <= 0 || TF_3.text.length <= 0 ||TF_4.text.length <= 0 || TF_5.text.length <= 0 || TF_6.text.length <= 0 ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"内容不能为空!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (!image1 || !image2) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"图片不能为空!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSDictionary *parame = [NSDictionary dictionaryWithObjectsAndKeys:TF_1.text,@"mulnews_title1",TF_3.text,@"mulnews_content1",TF_4.text,@"mulnews_title2",TF_6.text,@"mulnews_content2",nil];
    NSString *path = @"MobilePort/Index/group_mulnews.html";
    NSArray *imageArr = [NSArray arrayWithObjects:image1,image2, nil];
    NSMutableArray *NameArr = [NSMutableArray array];
    for (int i = 0; i<imageArr.count; i++) {
        [NameArr addObject:[NSString stringWithFormat:@"mulnews_img%d",i+1]];
    }
    [self.HUD show:YES];
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path imageArr:imageArr imageNameArr:NameArr parameters:parame finish:^(BOOL seccess, NSError *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            [self showText:@"新闻发送成功"];
            DLog(@"群发新闻成功了!");
            DLog(@"obj = %@",obj);
            DLog(@"info = %@",obj[@"info"]);
    
//        NSString *result  =[[ NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
//            DLog(@"result = %@",result);
//            NSArray *arr = [result componentsSeparatedByString:@","];
//            NSArray *infoArr = [arr[0] componentsSeparatedByString:@":"];
//            if (infoArr) {
//                DLog(@"info = %@",[infoArr lastObject]);
//            }
            
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            DLog(@"error = %@",error);
        }
    }];
}

#pragma mark --初始化菜单
-(void)AllocMenu {
    BtnArr = [NSMutableArray array];
    NSArray *titleArr = @[@"文字",@"图片",@"单条新闻",@"多条新闻"];
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetWidth(self.view.frame)/titleArr.count*i, 0, CGRectGetWidth(self.view.frame)/titleArr.count, 44);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setTitle:titleArr[i] forState:0];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(MenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.selected = YES;
        }
        [BtnArr addObject:btn];
        [_headView addSubview:btn];
        
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame), 2)];
    line.backgroundColor = [UIColor lightGrayColor];
    lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame)/titleArr.count, 2)];
    lineView.backgroundColor = [UIColor orangeColor];
    [_headView addSubview:line];
    [_headView addSubview:lineView];
    
}
#pragma mark --通过选中按钮获取当前菜单--
//获取当前菜单
-(NSInteger)GetModelIndex {
    __block NSInteger index;
    [BtnArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *btn = (UIButton *)obj;
        if (btn.selected) {
            index = idx;
        }
    }];
    return index;
}
#pragma mark --菜单按钮响应函数--
-(void)MenuBtnAction:(UIButton *)sender {
    int index = 0;
    for (int i = 0; i<BtnArr.count; i++) {
        UIButton *btn = BtnArr[i];
        btn.selected = NO;
        if (sender == BtnArr[i]) {
            index = i;
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        _ScrollView.contentOffset = CGPointMake(index * _ScrollView.frame.size.width, 0);
        sender.selected = YES;
        lineView.frame = CGRectMake(CGRectGetWidth(self.view.frame)/BtnArr.count*index, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame)/BtnArr.count, 2);
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark -
#pragma mark --CameraActionSheetDelegate--
- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (buttonIndex == 0){
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        } else {
            //                [self showText:@"无法打开相机"];
        }
    }
    [self presentModalController:picker animated:YES];
}
#pragma mark --UIImagePickerControllerDelegate--
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        img = [UIImage rotateImage:img];
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:img cropFrame:CGRectMake((self.view.frame.size.width - 240)/2, 100.0f, 240, 240) limitScaleRatio:1.0 title:@"上传素材"];
        [imgCropperVC setCompletionBlock:^(BOOL didFinished, UIImage *editedImage) {
            if (editedImage) {
                [SelectBtn setBackgroundImage:editedImage forState:UIControlStateNormal];
                [SelectBtn setTitle:@"" forState:UIControlStateNormal];
            }
        }];
        [self pushViewController:imgCropperVC];
    }];
}

#pragma mark -
#pragma mark --UIScrollViewDelegate--
// 当偏移量发生改变
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _ScrollView) {
        offset = scrollView.contentOffset.x/320;
        if (_oldOffsetX == scrollView.contentOffset.x ) {
            return;
        }
        if (_oldOffsetY == scrollView.contentOffset.y && _oldOffsetX != scrollView.contentOffset.x && _oldOffsetY == 0) {
            int index = 0;
            for (int i = 0; i<BtnArr.count; i++) {
                UIButton *btn = BtnArr[i];
                btn.selected = NO;
                if (i == offset) {
                    index = i;
                }
            }
            if (index == 3) {
                _headView.frame = CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame) + 60);
                [_tableView setTableHeaderView:_headView];
            }else {
                _headView.frame = CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame));
                [_tableView setTableHeaderView:_headView];
            }
            [UIView animateWithDuration:0.2 animations:^{
                UIButton *btn = BtnArr[index];
                btn.selected = YES;
                lineView.frame = CGRectMake(CGRectGetWidth(self.view.frame)/BtnArr.count*index, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame)/BtnArr.count, 2);
            } completion:^(BOOL finished) {
                
            }];
            
        }
        _oldOffsetY = scrollView.contentOffset.y;
        _oldOffsetX = scrollView.contentOffset.x;
    }
    
}
#pragma mark -
#pragma mark --tableViewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
//#pragma mark --设置表头视图--
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *headView = _headView;
//    return headView;
//}
//#pragma mark --设置表头的高度--
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
//    return CGRectGetHeight(headView.frame) + 60;
//}
#pragma mark -
#pragma mark --TextFieldDelegate--
#pragma mark --将要编辑时--
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    CGRect frame = textField.frame;
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset_y = (frame.origin.y+62 + 144)-(self.view.frame.size.height-256);//键盘高度256
    if (offset_y>0) {
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        [UIView animateWithDuration:0.3 animations:^{
//            _tableView.frame = CGRectMake(0.0f, -offset_y, self.view.frame.size.width, self.view.frame.size.height+offset_y);
            [_tableView setContentOffset:CGPointMake(0, offset_y) animated:YES];
        }];
        
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
#pragma mark  -- 编辑完成时 --
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [UIView animateWithDuration:0.3 animations:^{
//        _tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
        [_tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
